﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class EditProfile : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);
        String ID;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                try
                {
                    SqlCommand cmdUser;
                    SqlDataReader dtrUser;
                    conn.Open();

                    String username = Session["Username"].ToString();

                    cmdUser = new SqlCommand("Select * From Users where u_id=@UID ", conn);
                    cmdUser.Parameters.AddWithValue("@UID", Convert.ToInt32(Session["ID"].ToString()));


                    dtrUser = cmdUser.ExecuteReader();
                    dtrUser.Read();

                    ID = Session["ID"].ToString();
                    String userName = dtrUser["username"].ToString();
                    String password = dtrUser["password"].ToString();
                    String IC = dtrUser["u_ic"].ToString();
                    String age = dtrUser["u_age"].ToString();
                    String gender = dtrUser["u_gender"].ToString();

                    String address = dtrUser["u_address"].ToString();
                    string addressBeforeSplit = dtrUser["u_address"].ToString();
                    string[] lines = addressBeforeSplit.Split('|');


                    String phone = dtrUser["u_phone"].ToString();
                    String email = dtrUser["u_email"].ToString();
                    String rewardpoint = dtrUser["u_points"].ToString();
                    String UserStatus = dtrUser["u_status"].ToString();


                    lblName.Text = userName;
                    txtIC.Text = IC;
                    txtAge.Text = age;
                    rblGender.SelectedValue = gender;
                    txtAdd1.Text = lines[0].ToString();
                    txtAdd2.Text = lines[1].ToString();
                    txtPostCode.Text = lines[2].ToString();
                    txtCity.Text = lines[3].ToString();
                    txtState.Text = lines[4].ToString();
                    txtCountry.Text = lines[5].ToString();
                    txtPhone.Text = phone;
                    txtEmail.Text = email;
                    lblRewardPoints.Text = rewardpoint;

                    dtrUser.Close();
                    conn.Close();


                    if (UserStatus.Equals("Activate"))
                    {
                        btnMemberActivation.Text = "Deactivate My Account1";
                    }
                    else if (UserStatus.Equals("Deactivate"))
                    {
                        btnMemberActivation.Text = "Activate My Account1";

                    }



                }
                catch (SqlException ex)
                {
                    Response.Write("Invalid at Edit Member Profile - Page_Load");
                }

            }

        }

        public String return_userpicture()
        {
            SqlCommand cmdUser;
            SqlDataReader dtrUser;
            Response.Write(conn.State);
            conn.Open();

            cmdUser = new SqlCommand("Select * From Users WHERE uid=1", conn);
            dtrUser = cmdUser.ExecuteReader();
            dtrUser.Read();
            String userPicture = dtrUser["u_picture"].ToString();


            dtrUser.Close();
            conn.Close();

            return userPicture;

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            String saveDir = @"\uploads\products\";
            string appPath = Request.PhysicalApplicationPath;

            if (FileUpload1.HasFile)
            {
                string savePath = appPath + saveDir + Server.HtmlEncode(FileUpload1.FileName);
                String fileName = FileUpload1.FileName;

                FileUpload1.SaveAs(savePath);

                UploadStatusLabel.Text = "Your file was saved as ";
                imgName.Text = fileName;
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }
        }

        protected void UpdateMyProfile_Click(object sender, EventArgs e)
        {

            try
            {

                String IC = txtIC.Text;
                int Age = Convert.ToInt32(txtAge.Text);
                String Gender = rblGender.SelectedValue;
                String fullAddress = txtAdd1.Text + "|" + txtAdd2.Text + "|" + txtPostCode.Text + "|" + txtCity.Text + "|" + txtState.Text + "|" + txtCountry.Text;
                String Email = txtEmail.Text;
                String PhoneNo = txtPhone.Text;

                conn.Open();
           
                SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_ic=@Ic,u_age=@Age,u_gender=@Gender,u_address=@Address,u_email=@Email,u_phone=@Phone,u_picture=@Picture where u_id =@Id ", conn);
                //SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_ic=@Ic where u_id =@Id ", conn);

                cmdUpdate.Parameters.AddWithValue("@Ic", IC);
                cmdUpdate.Parameters.AddWithValue("@Age", Age);
                cmdUpdate.Parameters.AddWithValue("@Gender", Gender);
                cmdUpdate.Parameters.AddWithValue("@Address", fullAddress);
                cmdUpdate.Parameters.AddWithValue("@Phone", PhoneNo);
                cmdUpdate.Parameters.AddWithValue("@Email", Email);
                cmdUpdate.Parameters.AddWithValue("@Picture", imgName.Text);
                cmdUpdate.Parameters.AddWithValue("@Id", ID);

                int x = cmdUpdate.ExecuteNonQuery();

                Response.Write(x + "_xxx" + IC);
           
                conn.Close();
            }
            catch (SqlException ex)
            {

            }

        }


        protected void btnMemberActivation_Click(object sender, EventArgs e)
        {

            try
            {
                SqlCommand cmdUser;
                SqlDataReader dtrUser;
                conn.Open();

                String username = Session["Username"].ToString();

                cmdUser = new SqlCommand("Select * From Users where u_id=@UID ", conn);
                cmdUser.Parameters.AddWithValue("@UID", Convert.ToInt32(Session["ID"].ToString()));


                dtrUser = cmdUser.ExecuteReader();
                dtrUser.Read();

                String UserStatus = dtrUser["u_status"].ToString();

                dtrUser.Close();

                if(UserStatus.Equals("Activate"))
                {
                    
                    SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_status=@STATUS where u_id =@UID ", conn);
                    cmdUpdate.Parameters.AddWithValue("@STATUS", "Deactivate");
                    cmdUpdate.Parameters.AddWithValue("@UID", Convert.ToInt32(Session["ID"].ToString()));
                    cmdUpdate.ExecuteNonQuery();
                    btnMemberActivation.Text = "Activate My Account2";
                }
                else if (UserStatus.Equals("Deactivate"))
                {
                    SqlCommand cmdUpdate = new SqlCommand("Update Users SET u_status=@STATUS where u_id =@UID ", conn);
                    cmdUpdate.Parameters.AddWithValue("@STATUS", "Activate");
                    cmdUpdate.Parameters.AddWithValue("@UID", Convert.ToInt32(Session["ID"].ToString()));
                    cmdUpdate.ExecuteNonQuery();
                    btnMemberActivation.Text = "Deactivate My Account2";
                }


                conn.Close();

             

            }
            catch (SqlException ex)
            {


            }

        }

        protected void btnReservation_Click(object sender, EventArgs e)
        {
            Response.Redirect("MemberReservationManage.aspx");
        }
    }
}